/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * nihongo
 * Copyright (C) Massimiliano Maniscalco 2011, 2012 <massi.neptune@yahoo.com>
 * 
 * nihongo is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * nihongo is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef _CONTROLLER_H_
#define _CONTROLLER_H_

#include <gtkmm.h>
#include <giomm/file.h>
#include <iostream>
#include <fstream>
#include "parola.h"
#include "kanji.h"
#include "esempio.h"
#include "lezione.h"
#include "parole_list_model.h"
#include "popup_button_model.h"
#include "kanji_list_model.h"
#include "esempi_list_model.h"
#include "lezioni_list_model.h"
#include "test_controller.h"

#define STATUS_STARTING		0
#define STATUS_LOADING		1
#define STATUS_RUNNING		2

using namespace std;

class Controller
{
	
public:
	inline Controller () :
		_shouldSave ( false ), _startingApp ( STATUS_STARTING ), _paroleCaricate ( 0 ), _kanjiCaricati ( 0 ), _esempiCaricati ( 0 ), _lezioniCaricate ( 0 )
	{}
	inline Controller( Glib::RefPtr<Gtk::Builder> _bldr, Glib::RefPtr<Gtk::Application> _app );
	inline ~Controller ();
	inline Gtk::Window* main_window ( void ) { return _mainWin; }
	bool save_data ( const Glib::ustring &filename );
	inline bool should_save ( void ) { return _shouldSave; }
	inline Glib::ustring data_path ( void ) { return _dataFilename; }
		
protected:
	void load_data ( const Glib::ustring &filename );
	bool load_items_from_xml_file ( const Glib::ustring &filename );
	template <class ItemClass>
	int save_items_to_xml_file ( list<ItemClass> &_itemsList, ofstream *xmlstr );
	void connect_signals ( void );
	void on_cancel_button_clicked ( Gtk::Window *win );
	
	// main win
	bool on_main_win_focus_in ( GdkEventFocus *event );
	bool on_main_win_focus_out ( GdkEventFocus *event );
	void on_main_page_switch ( Gtk::Widget *page, guint page_num );
	void on_items_list_selection_changed ( void );
	void get_tree_row_ref ( const vector<Gtk::TreePath> &_sel, Glib::RefPtr<Gtk::TreeModel> _listModel, vector<Gtk::TreeRowReference> &_selRef );
	void load_pop_up_button ( vector<Glib::ustring>::iterator _start, vector<Glib::ustring>::iterator _end, Glib::RefPtr<Gtk::ListStore> _popUpList );
	// main toolbar and menu
	void on_import_action_clicked ( void );
	void on_export_action_clicked ( void );
	void on_save_action_clicked ( void );
	void on_add_action_clicked ( void );
	void on_remove_action_clicked ( void );
	void on_lectio_action_clicked ( void );
	void on_search_action_clicked ( void );
	void on_test_action_clicked ( void );
	void on_info_action_clicked ( void );
	void on_reset_action_clicked ( void );
	void on_quit_action_clicked ( void );
	void on_about_action_clicked ( void );
	void on_about_response ( int response_id, Gtk::AboutDialog *_about );
	void on_quick_search_icon_clicked ( Gtk::EntryIconPosition icon_position, const GdkEventButton* event);
	void on_quick_search_text_inserted ( guint position, const gchar* chars, guint n_chars );
	void on_quick_search_text_deleted ( guint position, guint n_chars );
	void on_quick_search_menu_toggled ( Gtk::RadioMenuItem *sender );
	// search item win
	void on_search_default_button_clicked ( void );
	void on_search_apply_button_clicked ( void );
	// info item win
	void populate_item_stats ( const Voce *vox );
	// page parole
	void load_parole_in_tree_view ( bool filtro = false );
	list<Parola>::iterator get_parola_iterator ( Gtk::TreeModel::Row _row );
	void parole_tree_remove_selected_rows ( const vector<Gtk::TreePath> &_sel );
	void on_parole_tree_double_clicked ( GdkEventButton *event );
	Voce* populate_parola_info ( Gtk::TreeModel::Row _row );
	void parole_tree_reset_selected_rows ( const vector<Gtk::TreePath> &_sel );
	void export_parole_to_xml ( ofstream *xmlstr, const vector<Gtk::TreePath> &_sel );
	// add parola win
	void on_add_parola_confirm_button_clicked ( void );
	// page kanji
	void load_kanji_in_tree_view ( bool filtro = false );
	list<Kanji>::iterator get_kanji_iterator ( Gtk::TreeModel::Row _row );
	void kanji_tree_remove_selected_rows ( const vector<Gtk::TreePath> &_sel );
	void on_kanji_tree_double_clicked ( GdkEventButton *event );
	Voce* populate_kanji_info ( Gtk::TreeModel::Row _row );
	void kanji_tree_reset_selected_rows ( const vector<Gtk::TreePath> &_sel );
	void export_kanji_to_xml ( ofstream *xmlstr, const vector<Gtk::TreePath> &_sel );
	// add kanji win
	void on_add_kanji_confirm_button_clicked ( void );
	// page esempi
	void load_esempi_in_tree_view ( bool filtro = false );
	list<Esempio>::iterator get_esempio_iterator ( Gtk::TreeModel::Row _row );
	void esempi_tree_remove_selected_rows ( const vector<Gtk::TreePath> &_sel );
	void on_esempi_tree_double_clicked ( GdkEventButton *event );
	void populate_esempio_info ( Gtk::TreeModel::Row _row );
	void export_esempi_to_xml ( ofstream *xmlstr, const vector<Gtk::TreePath> &_sel );
	// add esempio win
	void on_add_esempio_confirm_button_clicked ( void );
	// search esempi win
	void on_search_esempi_default_button_clicked ( void );
	void on_search_esempi_apply_button_clicked ( void );
	// page lezioni win
	void load_lezioni_in_tree_view ( bool filtro = false );
	list<Lezione>::iterator get_lezione_iterator ( Gtk::TreeModel::Row _row );
	void lezioni_tree_remove_selected_rows ( const vector<Gtk::TreePath> &_sel );
	void on_lezioni_tree_double_clicked ( GdkEventButton *event );
	void populate_lezione_info ( Gtk::TreeModel::Row _row );
	void export_lezioni_to_xml ( ofstream *xmlstr, const vector<Gtk::TreePath> &_sel );
	// add lezione win
	void on_add_lectio_confirm_button_clicked ( void );
	void load_parole_della_lezione_in_tree_view ( const Lezione &lectio, Glib::RefPtr<Gtk::ListStore> _listStore );
	void load_kanji_della_lezione_in_tree_view ( const Lezione &lectio, Glib::RefPtr<Gtk::ListStore> _listStore );
	void load_esempi_della_lezione_in_tree_view ( const Lezione &lectio );
	void on_lectio_contenuto_tree_button_dx_clicked ( GdkEventButton *event );
	void on_menu_lectio_remove_item_selected ( void );
	// add to lezione win
	void on_add_to_lectio_confirm_button_clicked ( void );
	void on_add_to_lectio_selection_changed ( void );
	// test controller
	void on_signal_items_changed ( void );
	// parser xml
	void on_signal_attribute_found ( const Glib::ustring lang );
	void on_signal_parola_loaded ( const Parola &verbum );
	void on_signal_kanji_loaded ( const Kanji &signum );
	void on_signal_esempio_loaded ( const Esempio &exemplum );
	void on_signal_lezione_loaded ( const Lezione &lectio );
	void on_signal_import_end ( void );

private:
	Glib::RefPtr<Gtk::Application> _thisApp;
	Glib::ustring sys_language;
	char _startingApp;
	bool _shouldSave;
	unsigned int _paroleCaricate;
	unsigned int _kanjiCaricati;
	unsigned int _esempiCaricati;
	unsigned int _lezioniCaricate;
	Glib::ustring _dataFilename;
	list<Parola> *_parole;
	list<Kanji> *_kanji;
	list<Esempio> *_esempi;
	list<Lezione> *_lezioni;
	Gtk::Window* _mainWin;
	Gtk::Notebook *_mainNotebook;
	Gtk::Statusbar *_mainStatus;
	unsigned short _mainPage;
	PopUpButtonModel* _buttonPopUpColumn;
	int _stsItemSelId;
	int _stsItemDiskId;
	vector<Glib::ustring> *_popUpParTipoStrings;
	vector<Glib::ustring> *_popUpCampoStrings;
	vector<Glib::ustring> *_popUpTrattiStrings;
	// main toolbar
	Gtk::Toolbar *_mainToolbar;
	Gtk::ToolButton *_saveToolButton;
	Gtk::ToolButton *_addToolButton;
	Gtk::ToolButton *_remToolButton;
	Gtk::ToolButton *_lectioToolButton;
	Gtk::ToolButton *_searchToolButton;
	Gtk::ToolButton *_infoToolButton;
	Gtk::ToolButton *_resetToolButton;
	Gtk::ToolButton *_testToolButton;
	Gtk::Entry *_quickSearchEntry;
	// main menu
	Gtk::MenuBar *_mainMenuBar;
	Gtk::MenuItem *_importMenuItem;
	Gtk::MenuItem *_exportMenuItem;
	Gtk::MenuItem *_saveMenuItem;
	Gtk::MenuItem *_quitMenuItem;
	Gtk::MenuItem *_addMenuItem;
	Gtk::MenuItem *_remMenuItem;
	Gtk::MenuItem *_lectioMenuItem;
	Gtk::MenuItem *_resetMenuItem;
	Gtk::MenuItem *_infoMenuItem;
	Gtk::MenuItem *_aboutMenuItem;
	// search item win
	Gtk::Window *_srchItemWin;
	Gtk::Frame *_srchItemFrame1;
	Gtk::Label *_srchItemLabel1;
	Glib::RefPtr<Gtk::ListStore> _srchItemListStore1;
	Gtk::ComboBox *_srchItemComboBox1;
	Gtk::Entry *_srchItemEntry2;
	Glib::RefPtr<Gtk::ListStore> _srchItemListStore3;
	Gtk::ComboBox *_srchItemComboBox3;
	Gtk::Button *_srchItemPulisciButton;
	Gtk::Button *_srchItemApplicaButton;
	// info item win
	Gtk::Window *_infoItemWin;
	Gtk::Label *_infoItemProLabel1;
	Gtk::Label *_infoItemProLabel2;
	Gtk::Label *_infoItemProLabel3;
	Gtk::Label *_infoItemProLabel4;
	Gtk::Label *_infoItemProLabel5;
	Gtk::Label *_infoItemProLabel6;
	Gtk::Label *_infoItemProLabel7;
	Gtk::Label *_infoItemProLabel8;
	Gtk::Label *_infoItemProValLabel1;
	Gtk::Label *_infoItemProValLabel2;
	Gtk::Label *_infoItemProValLabel3;
	Gtk::Label *_infoItemProValLabel4;
	Gtk::Label *_infoItemProValLabel5;
	Gtk::Label *_infoItemProValLabel6;
	Gtk::Label *_infoItemProValLabel7;
	Gtk::Label *_infoItemProValLabel8;
	Gtk::VBox *_infoItemStatsBox;
	Gtk::Label *_infoItemVal1Label1;
	Gtk::Label *_infoItemVal1Label2;
	Gtk::Label *_infoItemVal1Label3;
	Gtk::Label *_infoItemVal1Label4;
	Gtk::Label *_infoItemVal2Label1;
	Gtk::Label *_infoItemVal2Label2;
	Gtk::Label *_infoItemVal2Label3;
	Gtk::Label *_infoItemVal2Label4;
	Gtk::Label *_infoItemDate1Label1;
	Gtk::Label *_infoItemDate1Label2;
	Gtk::Label *_infoItemDate1Label3;
	Gtk::Label *_infoItemDate1Label4;
	Gtk::Label *_infoItemDate2Label1;
	Gtk::Label *_infoItemDate2Label2;
	Gtk::Label *_infoItemDate2Label3;
	Gtk::Label *_infoItemDate2Label4;
	Gtk::Label *_infoItemTotLabel;
	Gtk::TreeView *_infoItemList1;
	Gtk::TreeView *_infoItemList2;
	Glib::RefPtr<Gtk::ListStore> _infoItemKanjiListStore;
	Glib::RefPtr<Gtk::ListStore> _infoItemParoleListStore;
	Glib::RefPtr<Gtk::ListStore> _infoItemEsempiListStore;
	Gtk::Label *_infoItemTabLabel1;
	Gtk::Label *_infoItemTabLabel2;
	Gtk::TreeViewColumn *_infoItemList1Col1;
	Gtk::TreeViewColumn *_infoItemList1Col2;
	Gtk::TreeViewColumn *_infoItemList1Col3;
	Gtk::TreeViewColumn *_infoItemList2Col1;
	Gtk::TreeViewColumn *_infoItemList2Col2;
	Gtk::TreeViewColumn *_infoItemList2Col3;
	// page parole
	ParoleListModel* _paroleListColumns;
	Glib::RefPtr<Gtk::ListStore> _paroleListStore;
	Gtk::TreeView* _paroleDictList;
	Glib::RefPtr<Gtk::TreeSelection> _paroleDictSelection;
	// quick search parole menu
	Gtk::Menu *_quickSearchParoleMenu;
	Gtk::RadioMenuItem *_quickSearchParoleMenuItem1;
	Gtk::RadioMenuItem *_quickSearchParoleMenuItem2;
	Gtk::RadioMenuItem *_quickSearchParoleMenuItem3;
	Gtk::RadioMenuItem *_quickSearchParoleMenuItem4;
	Gtk::RadioMenuItem *_quickSearchParoleMenuItem5;
	// add parola win
	Gtk::Window* _addParWin;
	Glib::RefPtr<Gtk::ListStore> _tipoPopUpStore;
	Gtk::Entry *_newKotoba;
	Gtk::Entry *_newYomikata;
	Gtk::Entry *_newParTraduzione;
	Gtk::ComboBox *_newTipoCombobox;
	Gtk::Entry *_newParContesto;
	Gtk::Button *_newParConfButton;
	Gtk::Button *_newParAnnuButton;
	// page kanji
	KanjiListModel* _kanjiListColumns;
	Glib::RefPtr<Gtk::ListStore> _kanjiListStore;
	Gtk::TreeView* _kanjiDictList;
	Glib::RefPtr<Gtk::TreeSelection> _kanjiDictSelection;
	// quick search kanji menu
	Gtk::Menu *_quickSearchKanjiMenu;
	Gtk::RadioMenuItem *_quickSearchKanjiMenuItem1;
	Gtk::RadioMenuItem *_quickSearchKanjiMenuItem2;
	Gtk::RadioMenuItem *_quickSearchKanjiMenuItem3;
	Gtk::RadioMenuItem *_quickSearchKanjiMenuItem4;
	Gtk::RadioMenuItem *_quickSearchKanjiMenuItem5;
	Gtk::RadioMenuItem *_quickSearchKanjiMenuItem6;
	Gtk::RadioMenuItem *_quickSearchKanjiMenuItem7;
	// add kanji win
	Gtk::Window* _addKnjWin;
	Gtk::Entry* _newKanji;
	Gtk::Entry* _newRadicale;
	Gtk::Entry* _newTratti;
	Gtk::Entry* _newTrattiRadicale;
	Gtk::Entry* _newKunyomi;
	Gtk::Entry* _newOnyomi;
	Gtk::Entry* _newSignificato;
	Gtk::Entry* _newKnjContesto;
	Gtk::Button* _newKnjAnnullaButton;
	Gtk::Button* _newKnjConfermaButton;
	// page esempi
	EsempiListModel* _esempiListColumns;
	Glib::RefPtr<Gtk::ListStore> _esempiListStore;
	Gtk::TreeView* _esempiDictList;
	Glib::RefPtr<Gtk::TreeSelection> _esempiDictSelection;
	// quick search esempi menu
	Gtk::Menu *_quickSearchEsempiMenu;
	Gtk::RadioMenuItem *_quickSearchEsempiMenuItem1;
	Gtk::RadioMenuItem *_quickSearchEsempiMenuItem2;
	Gtk::RadioMenuItem *_quickSearchEsempiMenuItem3;
	Gtk::RadioMenuItem *_quickSearchEsempiMenuItem4;
	// add esempio win
	Gtk::Window *_addExaWin;
	Gtk::Entry *_newFrase;
	Gtk::Entry *_newExaTraduzione;
	Gtk::Entry *_newExaContesto;
	Gtk::Button *_newExaAnnullaButton;
	Gtk::Button *_newExaConfermaButton;
	// page lezioni
	LezioniListModel* _lezioniListColumns;
	Glib::RefPtr<Gtk::ListStore> _lezioniListStore;
	Gtk::TreeView* _lezioniDictList;
	Glib::RefPtr<Gtk::TreeSelection> _lezioniDictSelection;
	// quick search lezioni menu
	Gtk::Menu *_quickSearchLezioniMenu;
	Gtk::RadioMenuItem *_quickSearchLezioniMenuItem1;
	Gtk::RadioMenuItem *_quickSearchLezioniMenuItem2;
	Gtk::RadioMenuItem *_quickSearchLezioniMenuItem3;
	Gtk::RadioMenuItem *_quickSearchLezioniMenuItem4;
	Gtk::RadioMenuItem *_quickSearchLezioniMenuItem5;
	// add to lezione win
	Gtk::Window *_addToLezioneWin;
	Gtk::ComboBox *_addToLectioCombobox;
	Glib::RefPtr<Gtk::ListStore> _addToLectioListStore;
	Gtk::Entry *_addToLectioEntry;
	Gtk::Button *_addToLectioConfermaButton;
	Gtk::Button *_addToLectioAnnullaButton;
	// add lezione win
	Gtk::Window *_addLezWin;
	Gtk::Entry *_addLezEntry;
	Gtk::Notebook *_addLezNotebook;
	Glib::RefPtr<Gtk::ListStore> _addLezParListStore;
	Gtk::TreeView* _addLezParList;
	Glib::RefPtr<Gtk::TreeSelection> _lezioniParListSelection;
	Glib::RefPtr<Gtk::ListStore> _addLezKnjListStore;
	Gtk::TreeView* _addLezKnjList;
	Glib::RefPtr<Gtk::TreeSelection> _lezioniKnjListSelection;
	Glib::RefPtr<Gtk::ListStore> _addLezExaListStore;
	Gtk::TreeView* _addLezExaList;
	Glib::RefPtr<Gtk::TreeSelection> _lezioniExaListSelection;
	Gtk::Button *_addLezAnnullaButton;
	Gtk::Button *_addLezConfermaButton;
	Gtk::Menu *_lectioItemPopUpMenu;
	Gtk::MenuItem *_lectioItemMenuRemove;
	
	// test
	TestController *_testCtrl;
};

inline Controller::
Controller ( Glib::RefPtr<Gtk::Builder> _bldr, Glib::RefPtr<Gtk::Application> _app ) :
	_thisApp ( _app ), _shouldSave ( false ), _startingApp ( STATUS_STARTING ), _paroleCaricate ( 0 ), _kanjiCaricati ( 0 ), _esempiCaricati ( 0 ), _lezioniCaricate ( 0 )
{
	_parole = new list<Parola>;
	_kanji = new list<Kanji>;
	_esempi = new list<Esempio>;
	_lezioni = new list<Lezione>;
	Glib::ustring nihongoDirectory ( Glib::get_home_dir () + Glib::ustring ( "/.nihongo" ) );
	Glib::RefPtr<Gio::File> nihonDir = Gio::File::create_for_path ( nihongoDirectory );
	if ( !nihonDir->query_exists () )
		nihonDir->make_directory ();
	_dataFilename = nihongoDirectory + Glib::ustring ( "/nihongo_data.xml" );
	sys_language = Glib::getenv ( Glib::ustring ( "LANG" ) );
	size_t pos = sys_language.find ( Glib::ustring ( "." ) );
	sys_language.assign ( sys_language.substr ( 0, pos ) );
	_bldr->get_widget("mainWindow", _mainWin);
	_bldr->get_widget ("mainNotebook", _mainNotebook);
	_mainPage = 0;
	_bldr->get_widget ("mainStatusbar", _mainStatus);
	_stsItemSelId = _mainStatus->get_context_id ( Glib::ustring ( "ITEM_SEL" ) );
	_stsItemDiskId = _mainStatus->get_context_id ( Glib::ustring ( "ITEM_DISK" ) );
	_buttonPopUpColumn = new PopUpButtonModel ();
	_paroleListColumns = new ParoleListModel ();
	_kanjiListColumns = new KanjiListModel ();
	_esempiListColumns = new EsempiListModel ();
	// main toolbar
	_bldr->get_widget ("mainToolbar", _mainToolbar);
	_bldr->get_widget ("saveToolButton", _saveToolButton);
	_bldr->get_widget ("addItemButton", _addToolButton);
	_bldr->get_widget ("remItemButton", _remToolButton);
	_bldr->get_widget ("lezioneItemButton", _lectioToolButton);
	_bldr->get_widget ("searchItemButton", _searchToolButton);
	_bldr->get_widget ("infoItemButton", _infoToolButton);
	_bldr->get_widget ("resetToolButton", _resetToolButton);
	_bldr->get_widget ("testToolButton", _testToolButton);
	_bldr->get_widget ("quickSearchEntry", _quickSearchEntry);
	// main menu
	_bldr->get_widget ("mainMenubar", _mainMenuBar);
	_bldr->get_widget ("fileMenuItem2", _importMenuItem);
	_bldr->get_widget ("fileMenuItem3", _exportMenuItem);
	_bldr->get_widget ("fileMenuItem1", _saveMenuItem);
	_bldr->get_widget ("fileMenuItem7", _quitMenuItem);
	_bldr->get_widget ("voceMenuItem1", _addMenuItem);
	_bldr->get_widget ("voceMenuItem2", _remMenuItem);
	_bldr->get_widget ("voceMenuItem3", _lectioMenuItem);
	_bldr->get_widget ("voceMenuItem4", _resetMenuItem);
	_bldr->get_widget ("voceMenuItem5", _infoMenuItem);
	_bldr->get_widget ("aiutoMenuItem2", _aboutMenuItem);
	// search item win
	_bldr->get_widget ("searchItemWin", _srchItemWin);
	_bldr->get_widget ("searchItemFrame1", _srchItemFrame1);
	_bldr->get_widget ("searchItemFrameLabel1", _srchItemLabel1);
	_srchItemListStore1 = Gtk::ListStore::create(*_buttonPopUpColumn);
	_bldr->get_widget ("searchItemCombobox1", _srchItemComboBox1);
	_srchItemComboBox1->set_model(_srchItemListStore1);
	Glib::ustring _sipu1[11] = { "qualunque", "nome", "agg.vo i", "agg.vo na", "v. godan tr.", "v. godan intr.", "v. ichidan tr.", "v. ichidan intr.", "v. irregolare", "posposizione", "avverbio" };
	_popUpParTipoStrings = new vector<Glib::ustring> ( _sipu1, _sipu1 + 11 );
	load_pop_up_button ( _popUpParTipoStrings->begin(), _popUpParTipoStrings->end(), _srchItemListStore1 );
	_srchItemComboBox1->set_active ( 0 );
	_bldr->get_widget ("searchItemEntry2", _srchItemEntry2);
	_srchItemListStore3 = Gtk::ListStore::create(*_buttonPopUpColumn);
	_bldr->get_widget ("searchItemCombobox3", _srchItemComboBox3);
	_srchItemComboBox3->set_model(_srchItemListStore3);
	Glib::ustring _sipu2[21] = { "qualunque", "parola", "言葉", "読み方", "contesto", "qualunque", "kanji", "radicale", "onyomi", "kunyomi", "significato", "contesto", "qualunque", "frase giapponese", "frase italiano", "contesto", "qualunque", "nome", "parole (言葉)", "kanji (漢字)", "esempi (日本語)" };
	_popUpCampoStrings = new vector<Glib::ustring> ( _sipu2, _sipu2 + 21 );
	load_pop_up_button ( _popUpCampoStrings->begin(), _popUpCampoStrings->end() - 16, _srchItemListStore3 );
	_srchItemComboBox3->set_active ( 0 );
	Glib::ustring _sipu3[21] = { "qualunque", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
	_popUpTrattiStrings = new vector<Glib::ustring> ( _sipu3, _sipu3 + 21 );
	_bldr->get_widget ("searchItemDefaultButton", _srchItemPulisciButton);
	_bldr->get_widget ("searchItemApplicaButton", _srchItemApplicaButton);
	// info item win
	_bldr->get_widget ("infoItemWin", _infoItemWin);
	_bldr->get_widget ("infoItemPrLabel1", _infoItemProLabel1);
	_infoItemProLabel1->set_text ( Glib::ustring ( "言葉" ) );
	_bldr->get_widget ("infoItemPrLabel3", _infoItemProLabel2);
	_infoItemProLabel2->set_text ( Glib::ustring ( "読み方" ) );
	_bldr->get_widget ("infoItemPrLabel5", _infoItemProLabel3);
	_infoItemProLabel3->set_text ( Glib::ustring ( "Traduzione" ) );
	_bldr->get_widget ("infoItemPrLabel7", _infoItemProLabel4);
	_infoItemProLabel4->set_text ( Glib::ustring ( "Tipo" ) );
	_bldr->get_widget ("infoItemPrLabel9", _infoItemProLabel5);
	_infoItemProLabel5->set_text ( Glib::ustring ( "Contesto" ) );
	_bldr->get_widget ("infoItemPrLabel11", _infoItemProLabel6);
	_bldr->get_widget ("infoItemPrLabel13", _infoItemProLabel7);
	_bldr->get_widget ("infoItemPrLabel15", _infoItemProLabel8);
	_bldr->get_widget ("infoItemPrLabel2", _infoItemProValLabel1);
	_bldr->get_widget ("infoItemPrLabel4", _infoItemProValLabel2);
	_bldr->get_widget ("infoItemPrLabel6", _infoItemProValLabel3);
	_bldr->get_widget ("infoItemPrLabel8", _infoItemProValLabel4);
	_bldr->get_widget ("infoItemPrLabel10", _infoItemProValLabel5);
	_bldr->get_widget ("infoItemPrLabel12", _infoItemProValLabel6);
	_bldr->get_widget ("infoItemPrLabel14", _infoItemProValLabel7);
	_bldr->get_widget ("infoItemPrLabel16", _infoItemProValLabel8);
	_bldr->get_widget ("infoItemVbox3", _infoItemStatsBox);
	_bldr->get_widget ("infoItemValLabel1", _infoItemVal1Label1);
	_bldr->get_widget ("infoItemValLabel3", _infoItemVal1Label2);
	_bldr->get_widget ("infoItemValLabel5", _infoItemVal1Label3);
	_bldr->get_widget ("infoItemValLabel7", _infoItemVal1Label4);
	_bldr->get_widget ("infoItemValLabel2", _infoItemVal2Label1);
	_bldr->get_widget ("infoItemValLabel4", _infoItemVal2Label2);
	_bldr->get_widget ("infoItemValLabel6", _infoItemVal2Label3);
	_bldr->get_widget ("infoItemValLabel8", _infoItemVal2Label4);
	_bldr->get_widget ("infoItemDateLabel1", _infoItemDate1Label1);
	_bldr->get_widget ("infoItemDateLabel3", _infoItemDate1Label2);
	_bldr->get_widget ("infoItemDateLabel5", _infoItemDate1Label3);
	_bldr->get_widget ("infoItemDateLabel7", _infoItemDate1Label4);
	_bldr->get_widget ("infoItemDateLabel2", _infoItemDate2Label1);
	_bldr->get_widget ("infoItemDateLabel4", _infoItemDate2Label2);
	_bldr->get_widget ("infoItemDateLabel6", _infoItemDate2Label3);
	_bldr->get_widget ("infoItemDateLabel8", _infoItemDate2Label4);
	_bldr->get_widget ("infoItemTotTestLabel", _infoItemTotLabel);
	_infoItemKanjiListStore = Gtk::ListStore::create(*_kanjiListColumns);
	_infoItemParoleListStore = Gtk::ListStore::create(*_paroleListColumns);
	_infoItemEsempiListStore = Gtk::ListStore::create(*_esempiListColumns);
	_bldr->get_widget ("infoItemList1", _infoItemList1);
	_infoItemList1->set_model (_infoItemKanjiListStore);
	_bldr->get_widget ("infoItemNoteLabel2", _infoItemTabLabel1);
	_infoItemTabLabel1->set_text ( Glib::ustring ( "漢字" ) );
	_bldr->get_widget ("infoItemNoteLabel3", _infoItemTabLabel2);
	_infoItemTabLabel2->set_text ( Glib::ustring ( "Esempi" ) );
	_bldr->get_widget ("infoItemList2", _infoItemList2);
	_infoItemList2->set_model (_infoItemEsempiListStore);
	_infoItemList1Col1 = _infoItemList1->get_column ( 0 );
	_infoItemList1Col2 = _infoItemList1->get_column ( 1 );
	_infoItemList1Col3 = _infoItemList1->get_column ( 2 );
	_infoItemList2Col1 = _infoItemList2->get_column ( 0 );
	_infoItemList2Col2 = _infoItemList2->get_column ( 1 );
	_infoItemList2Col3 = _infoItemList2->get_column ( 2 );
	// page parole
	_paroleListStore = Gtk::ListStore::create(*_paroleListColumns);
	_bldr->get_widget ("paroleListView", _paroleDictList);
	_paroleDictList->set_model(_paroleListStore);
	_paroleDictSelection = _paroleDictList->get_selection ();
	_paroleDictSelection->set_mode (Gtk::SELECTION_MULTIPLE);
	// quick search parole menu
	_bldr->get_widget ("quickSearchParoleMenu", _quickSearchParoleMenu);
	_bldr->get_widget ("qsearchParoleMenuitem1", _quickSearchParoleMenuItem1);
	_bldr->get_widget ("qsearchParoleMenuitem2", _quickSearchParoleMenuItem2);
	_bldr->get_widget ("qsearchParoleMenuitem3", _quickSearchParoleMenuItem3);
	_bldr->get_widget ("qsearchParoleMenuitem4", _quickSearchParoleMenuItem4);
	_bldr->get_widget ("qsearchParoleMenuitem5", _quickSearchParoleMenuItem5);
	// page kanji
	_kanjiListStore = Gtk::ListStore::create(*_kanjiListColumns);
	_bldr->get_widget ("kanjiListView", _kanjiDictList);
	_kanjiDictList->set_model (_kanjiListStore);
	_kanjiDictSelection = _kanjiDictList->get_selection ();
	_kanjiDictSelection->set_mode (Gtk::SELECTION_MULTIPLE);
	// quick search kanji menu
	_bldr->get_widget ("quickSearchKanjiMenu", _quickSearchKanjiMenu);
	_bldr->get_widget ("qsearchKanjiMenuitem1", _quickSearchKanjiMenuItem1);
	_bldr->get_widget ("qsearchKanjiMenuitem2", _quickSearchKanjiMenuItem2);
	_bldr->get_widget ("qsearchKanjiMenuitem3", _quickSearchKanjiMenuItem3);
	_bldr->get_widget ("qsearchKanjiMenuitem4", _quickSearchKanjiMenuItem4);
	_bldr->get_widget ("qsearchKanjiMenuitem5", _quickSearchKanjiMenuItem5);
	_bldr->get_widget ("qsearchKanjiMenuitem6", _quickSearchKanjiMenuItem6);
	_bldr->get_widget ("qsearchKanjiMenuitem7", _quickSearchKanjiMenuItem7);
	// page esempi
	_esempiListStore = Gtk::ListStore::create(*_esempiListColumns);
	_bldr->get_widget ("esempiListView", _esempiDictList);
	_esempiDictList->set_model(_esempiListStore);
	_esempiDictSelection = _esempiDictList->get_selection ();
	_esempiDictSelection->set_mode (Gtk::SELECTION_MULTIPLE);
	// quick search esempi menu
	_bldr->get_widget ("quickSearchEsempiMenu", _quickSearchEsempiMenu);
	_bldr->get_widget ("qsearchEsempiMenuitem1", _quickSearchEsempiMenuItem1);
	_bldr->get_widget ("qsearchEsempiMenuitem2", _quickSearchEsempiMenuItem2);
	_bldr->get_widget ("qsearchEsempiMenuitem3", _quickSearchEsempiMenuItem3);
	_bldr->get_widget ("qsearchEsempiMenuitem4", _quickSearchEsempiMenuItem4);
	// page lezioni
	_lezioniListColumns = new LezioniListModel ();
	_lezioniListStore = Gtk::ListStore::create(*_lezioniListColumns);
	_bldr->get_widget ("lezioniListView", _lezioniDictList);
	_lezioniDictList->set_model(_lezioniListStore);
	_lezioniDictSelection = _lezioniDictList->get_selection ();
	// quick search lezioni menu
	_bldr->get_widget ("quickSearchLezioniMenu", _quickSearchLezioniMenu);
	_bldr->get_widget ("qsearchLezioniMenuitem1", _quickSearchLezioniMenuItem1);
	_bldr->get_widget ("qsearchLezioniMenuitem2", _quickSearchLezioniMenuItem2);
	_bldr->get_widget ("qsearchLezioniMenuitem3", _quickSearchLezioniMenuItem3);
	_bldr->get_widget ("qsearchLezioniMenuitem4", _quickSearchLezioniMenuItem4);
	_bldr->get_widget ("qsearchLezioniMenuitem5", _quickSearchLezioniMenuItem5);
	// add parola win
	_bldr->get_widget("addNewParWin", _addParWin);
	_tipoPopUpStore = Gtk::ListStore::create(*_buttonPopUpColumn);
	_bldr->get_widget ("newKotobaEntry", _newKotoba);
	_bldr->get_widget ("newYomikataEntry", _newYomikata);
	_bldr->get_widget ("addParTraduzioneEntry", _newParTraduzione);
	_bldr->get_widget ("newTipoCombobox", _newTipoCombobox);
	_newTipoCombobox->set_model(_tipoPopUpStore);
	load_pop_up_button ( _popUpParTipoStrings->begin() + 1, _popUpParTipoStrings->end(), _tipoPopUpStore );
	_bldr->get_widget ("newContestoEntry", _newParContesto);
	_bldr->get_widget ("confAddParButton", _newParConfButton);
	_bldr->get_widget ("annuAddParButton", _newParAnnuButton);
	// add kanji win
	_bldr->get_widget ("addNewKnjWin", _addKnjWin);
	_bldr->get_widget ("addKnjKnjEntry", _newKanji);
	_bldr->get_widget ("addKnjRadEntry", _newRadicale);
	_bldr->get_widget ("addKnjTrKnEntry", _newTratti);
	_bldr->get_widget ("addKnjTrRaEntry", _newTrattiRadicale);
	_bldr->get_widget ("addKnjKunEntry", _newKunyomi);
	_bldr->get_widget ("addKnjOnEntry", _newOnyomi);
	_bldr->get_widget ("addKnjSigEntry", _newSignificato);
	_bldr->get_widget ("addKnjConEntry", _newKnjContesto);
	_bldr->get_widget ("addKnjAnnullaButton", _newKnjAnnullaButton);
	_bldr->get_widget ("addKnjConfermaButton", _newKnjConfermaButton);
	// add esempio win
	_bldr->get_widget ("addNewExaWin", _addExaWin);
	_bldr->get_widget ("addExaFraseEntry", _newFrase);
	_bldr->get_widget ("addExaTradEntry", _newExaTraduzione);
	_bldr->get_widget ("addExaContestoEntry", _newExaContesto);
	_bldr->get_widget ("addExaAnnullaButton", _newExaAnnullaButton);
	_bldr->get_widget ("addExaConfermaButton", _newExaConfermaButton);
	// add to lezione win
	_bldr->get_widget ("addToLessonWin", _addToLezioneWin);
	_addToLectioListStore = Gtk::ListStore::create(*_buttonPopUpColumn);
	_bldr->get_widget ("addToLsnCombobox1", _addToLectioCombobox);
	_addToLectioCombobox->set_model (_addToLectioListStore);
	_bldr->get_widget ("addToLsnEntry1", _addToLectioEntry);
	_bldr->get_widget ("addToLsnAnnullaButton", _addToLectioAnnullaButton);
	_bldr->get_widget ("addToLsnConfermaButton", _addToLectioConfermaButton);
	// add lezione win
	_bldr->get_widget ("addNewLezWin", _addLezWin);
	_bldr->get_widget ("addNewLezEntry1", _addLezEntry);
	_bldr->get_widget ("addNewLezNotebook1", _addLezNotebook);
	_addLezParListStore = Gtk::ListStore::create(*_paroleListColumns);
	_bldr->get_widget ("addNewLezParList", _addLezParList);
	_addLezParList->set_model ( _addLezParListStore );
	_lezioniParListSelection = _addLezParList->get_selection ();
	_lezioniParListSelection->set_mode (Gtk::SELECTION_MULTIPLE);
	_addLezKnjListStore = Gtk::ListStore::create(*_kanjiListColumns);
	_bldr->get_widget ("addNewLezKnjList", _addLezKnjList);
	_addLezKnjList->set_model ( _addLezKnjListStore );
	_lezioniKnjListSelection = _addLezKnjList->get_selection ();
	_lezioniKnjListSelection->set_mode (Gtk::SELECTION_MULTIPLE);
	_addLezExaListStore = Gtk::ListStore::create(*_esempiListColumns);
	_bldr->get_widget ("addNewLezExaList", _addLezExaList);
	_addLezExaList->set_model ( _addLezExaListStore );
	_lezioniExaListSelection = _addLezExaList->get_selection ();
	_lezioniExaListSelection->set_mode (Gtk::SELECTION_MULTIPLE);
	_bldr->get_widget ("addNewLezButton1", _addLezAnnullaButton);
	_bldr->get_widget ("addNewLezButton2", _addLezConfermaButton);
	_bldr->get_widget ("listaItemLectioPopUpMenu", _lectioItemPopUpMenu);
	_bldr->get_widget ("lectioItem1", _lectioItemMenuRemove);

	_testCtrl = new TestController ( _bldr );
	connect_signals ();
}

inline Controller::
~Controller ()
{
	delete _popUpParTipoStrings;
	delete _popUpCampoStrings;
	delete _popUpTrattiStrings;
	delete _paroleListColumns;
	delete _kanjiListColumns;
	delete _esempiListColumns;
	delete _lezioniListColumns;
	delete _buttonPopUpColumn;
	delete _testCtrl;
	delete _parole;
	delete _kanji;
	delete _esempi;
	delete _lezioni;
}

#endif // _CONTROLLER_H_